package cn.anecansaitin.not_enough_harness.common.capability;

import cn.anecansaitin.not_enough_harness.common.capability.harness_attribute.HarnessAttributeHandler;
import cn.anecansaitin.not_enough_harness.common.capability.harness_capability.HarnessCapabilityHandler;
import cn.anecansaitin.not_enough_harness.common.harness_data.HarnessSpeed;
import cn.anecansaitin.not_enough_harness.common.harness_data.SprintableHarness;
import cn.anecansaitin.not_enough_harness.common.harness_data.HarnessCapabilityCD;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.HappyGhast;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public class SprintableHarnessHandler implements HarnessCapabilityHandler, HarnessAttributeHandler {
    public static final SprintableHarnessHandler INSTANCE = new SprintableHarnessHandler();

    @Override
    public void tick(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            HarnessCapabilityCD.set(harness, harnessCapabilityCD.cd(), harnessCapabilityCD.duration() - 1);
        } else if (harnessCapabilityCD.cd() > 0) {
            HarnessCapabilityCD.set(harness, harnessCapabilityCD.cd() - 1, 0);
        }
    }

    @Override
    public void use(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.cd() > 0) {
            return;
        }

        SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD.set(harness, sprintableHarness.cd(), sprintableHarness.duration());
    }

    @Override
    public float forwardSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.forwardSpeed() + harnessSpeed.forwardSpeed();
        } else {
            return harnessSpeed.forwardSpeed();
        }
    }

    @Override
    public float backwardSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.backwardSpeed() + harnessSpeed.backwardSpeed();
        } else {
            return harnessSpeed.backwardSpeed();
        }
    }

    @Override
    public float leftSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.leftSpeed() + harnessSpeed.leftSpeed();
        } else {
            return harnessSpeed.leftSpeed();
        }
    }

    @Override
    public float rightSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.rightSpeed() + harnessSpeed.rightSpeed();
        } else {
            return harnessSpeed.rightSpeed();
        }
    }

    @Override
    public float upSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.upSpeed() + harnessSpeed.upSpeed();
        } else {
            return harnessSpeed.upSpeed();
        }
    }

    @Override
    public float downSpeed(ItemStack harness, HappyGhast happyGhast, Player driver) {
        HarnessSpeed harnessSpeed = HarnessSpeed.getComponentOrDataMapAndSet(harness);
        HarnessCapabilityCD harnessCapabilityCD = HarnessCapabilityCD.get(harness);

        if (harnessCapabilityCD.duration() > 0) {
            SprintableHarness sprintableHarness = SprintableHarness.getComponentOrDataMapAndSet(harness);
            return sprintableHarness.downSpeed() + harnessSpeed.downSpeed();
        } else {
            return harnessSpeed.downSpeed();
        }
    }
}
